package de.fhkl.gatav.ut.raindrops_1;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;

class Drop {
	private float x, y, speed, size;
	private Bitmap dropBitmap;
	public static final int RADIUS = 6;
	public static final int MAX_SPEED = 10;
	public static final int MIN_SPEED = 1;
	public static final float SPEED_CHANGE_RATE = (float)0.01;
	private float amountOfWobble = 0;
	private static final float DOUBLE_PI = (float)(2*3.141529);
	public static final float WOBBLE_RATE = (float)8*DOUBLE_PI;
	public static final float WOBBLE_AMOUNT = (float)0.05;
	private float amountOfShift = 0;
	public static final float SHIFT_RATE = (float)50*DOUBLE_PI;
	public static final float SHIFT_AMOUNT = (float)2;

	public Drop (float x, float y, float speed, Bitmap dropBitmap) {
		this.x = x;
		this.y = y;
		this.size = RADIUS*(float)Math.random();
		this.speed = Math.max(speed, MIN_SPEED);
		this.dropBitmap = dropBitmap;
	}

	public void draw(Canvas c) {
		float xDeformation = size*WOBBLE_AMOUNT*amountOfWobble;
		float yDeformation = size*WOBBLE_AMOUNT*(1-amountOfWobble);
		float shift = SHIFT_AMOUNT*amountOfShift;
		
		Rect nullRect = null;
		Paint nullPaint = null;
		c.drawBitmap(dropBitmap, 
					 nullRect, 
					 new RectF(x-(size+xDeformation)+shift,
							   y-(size+yDeformation),
							   x+(size+xDeformation)+shift,
							   y+(size+yDeformation)),
					 nullPaint);
	}

	public void move(Canvas c, float numberOfFrames) {
		if ( this.size >= RADIUS ) {
			float movedDistance = speed*numberOfFrames;		//Now movement depends on time between frames
			speed += SPEED_CHANGE_RATE*speed*(MAX_SPEED-speed);
			y += movedDistance;
			amountOfWobble = (float)Math.cos ((y/c.getHeight())*WOBBLE_RATE);
			amountOfShift = (float)Math.sin ((y/c.getHeight())*SHIFT_RATE*movedDistance/MAX_SPEED);
		} else {
			size *= (1.0+Math.random()/RADIUS);
		}
	}

	public boolean outOfRange(Canvas c) {
		return (y-RADIUS > c.getHeight());
	}
}